package com.kuzan.autocloud.client.sys.helper;

import com.kuzan.autocloud.client.sys.util.HttpRequestUtil;
import com.kuzan.autocloud.common.model.organization.Person;
import com.kuzan.autocloud.common.repository.organization.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * <p>Description: </p>
 *
 * @author kuzank 2019/12/2
 */
@Component
public class HttpRequestHelper {

    @Autowired
    private PersonRepository personRepository;

    /**
     * TODO 若用户不存在则抛出异常，系统统一添加异常拦截器，用于捕获后返回未登录提醒
     *
     * @param servletRequest
     * @return
     */
    public Person getPerson(HttpServletRequest servletRequest) {

        String id = HttpRequestUtil.getPeople(servletRequest);

        Optional<Person> optionalPerson = personRepository.findById(id);
        if (optionalPerson.isPresent()) {
            return optionalPerson.get();
        }
        return null;
    }

}
